package se.energimyndigheten.lampguiden.bulbreplacement;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import se.energimyndigheten.lampguiden.LightSource;
import se.energimyndigheten.lampguiden.R;

public class ResultCardAdapter extends RecyclerView.Adapter<ResultCardAdapter.ViewHolder>{

    LightSource[] mLights;
    ComparisonCalculator mCalculator;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle;
        private ImageView[] mEnergyClass = new ImageView[2];
        private TextView mPrice;
        private TextView mSavings;
        private Button mChangeBtn;

        public ViewHolder(View v) {
            super(v);
            mTitle = (TextView) v.findViewById(R.id.title);
            mEnergyClass[0] = (ImageView) v.findViewById(R.id.energyclass1);
            mEnergyClass[1] = (ImageView) v.findViewById(R.id.energyclass2);
            mPrice = (TextView) v.findViewById(R.id.label_1);
            mSavings = (TextView) v.findViewById(R.id.label_3);
            mChangeBtn = (Button) v.findViewById(R.id.result_card_btn_change_price);
        }
    }

    public ResultCardAdapter(LightSource[] lights, ComparisonCalculator calculator) {
        mLights = lights;
        mCalculator = calculator;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.socket_result_card_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        final LightSource light = mLights[position];
        int[] energyClassRes = selectDrawable(light.getEnergyClass());
        int savings = mCalculator.getCostComparison(light);
        // show 0 kr if negative
        savings = savings < 0 ? 0: savings;

        viewHolder.mTitle.setText(light.getName());
        viewHolder.mEnergyClass[0].setImageResource(energyClassRes[0]);
        viewHolder.mEnergyClass[0].setContentDescription(drawableDescription(energyClassRes[0], viewHolder.itemView));
        if (energyClassRes[1] != -1) {
            viewHolder.mEnergyClass[1].setImageResource(energyClassRes[1]);
            viewHolder.mEnergyClass[1].setContentDescription(drawableDescription(energyClassRes[1], viewHolder.itemView));
        }
        viewHolder.mPrice.setText(viewHolder.itemView.getResources().getString(R.string.result_price_text, light.getPrice()));
        viewHolder.mSavings.setText("" + savings + " kr");
        viewHolder.mChangeBtn.setTag(light);
    }

    private int[] selectDrawable(String type) {
        int [] res = new int[] {-1, -1};
        if ("A++-A".equals(type))
            res[0] = R.drawable.ic_arrow_a_plus_plus;
            res[1] = R.drawable.ic_arrow_a;
        if ("A+-A".equals(type))
            res[0] = R.drawable.ic_arrow_a_plus;
            res[1] = R.drawable.ic_arrow_a;
        if ("B".equals(type))
            res[0] =  R.drawable.ic_arrow_b;
        if ("C".equals(type))
            res [0] =  R.drawable.ic_arrow_c;
        if ("C-D".equals(type)) {
            res = new int[] {R.drawable.ic_arrow_c, R.drawable.ic_arrow_d};
        }
        return res;
    }

    private String drawableDescription(int  d, View parent) {
        int stringId=0;
        switch (d) {
            case R.drawable.ic_arrow_a_plus_plus:
                stringId = R.string.ic_energi_aplusplus_a;
                break;
            case R.drawable.ic_arrow_a_plus:
                stringId = R.string.ic_energi_aplus_a;
                break;
            case R.drawable.ic_arrow_b:
                stringId = R.string.resultat_b;
                break;
            case R.drawable.ic_arrow_c:
                stringId = R.string.resultat_c;
                break;
            case R.drawable.ic_arrow_d:
                stringId = R.string.resultat_cd;
                break;
        }
        if (stringId==0)
            return null;
        return parent.getContext().getResources().getString(stringId);
    }

    @Override
    public int getItemCount() {
        return mLights.length;
    }
}
